﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Guedes_Library;
using System.Web.UI.HtmlControls;


public partial class produto_balde_gelita : System.Web.UI.Page
{
    int prodid;
    string nome, ext;


    protected void Page_Load(object sender, EventArgs e)
    {

        if (Request.QueryString["p"] == null)
        {
            Response.Redirect("produto_balde_gelita.aspx?p=1");
        }


        if (Session["idproduto"] == null)
        {
            var prodID = 0;
            using (guedesEntities _db = new guedesEntities())
            {
                prodID = (from p in _db.product
                          where p.ID_Cat == 4
                          orderby p.ID ascending
                          select p.ID).First();

            }
            using (guedesEntities _db = new guedesEntities())
            {
                var ID = (from p in _db.byproduct
                          where p.ID_Prod == prodID
                          orderby p.ID ascending
                          select p.ID_Prod).FirstOrDefault();

                Session["idproduto"] = ID;
            }

        }

        Session["auxComent"] = Session["idproduto"];


        if (!Page.IsPostBack)
        {

            Repeater1.DataSource = GetComents();
            Repeater1.DataBind();

            Repeater2.DataSource = GetProducts();
            Repeater2.DataBind();
        }
        Session.Remove("idproduto");
    }

    protected List<coments> GetComents()
    {
        List<coments> comentarios;
        prodid = int.Parse(Session["idproduto"].ToString());

        using (guedesEntities _db = new guedesEntities())
        {
            comentarios = (from p in _db.coments
                           where p.ID_Prod == prodid && p.Permission == true
                           orderby p.ID
                           select p).ToList();
        }
        return comentarios;
    }

    protected List<byproduct> GetProducts()
    {
        List<byproduct> produtos;

        using (guedesEntities _db = new guedesEntities())
        {
            produtos = (from p in _db.byproduct
                        where p.ID_Prod == prodid
                        orderby p.ID descending
                        select p).Take(4).ToList();

            if (produtos.Count > 0)
            {

                var firstImage = (from p in _db.byproduct
                                  where p.ID_Prod == prodid
                                  orderby p.ID descending
                                  select p.Img).First();

                HiddenImg.Value = firstImage;
            }
        }
        return produtos;
    }

    protected void Repeater1_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Label lbl1 = (Label)e.Item.FindControl("lblNome");
        lbl1.Text = (e.Item.DataItem as coments).Name;

        System.Web.UI.WebControls.Image img1 = (System.Web.UI.WebControls.Image)e.Item.FindControl("rating");
        if ((e.Item.DataItem as coments).Rating == 1)
        {
            img1.ImageUrl = "./galeria/estrela1.png";
        }
        if ((e.Item.DataItem as coments).Rating == 2)
        {
            img1.ImageUrl = "./galeria/estrela2.png";
        }
        if ((e.Item.DataItem as coments).Rating == 3)
        {
            img1.ImageUrl = "./galeria/estrela3.png";
        }
        if ((e.Item.DataItem as coments).Rating == 4)
        {
            img1.ImageUrl = "./galeria/estrela4.png";
        }
        if ((e.Item.DataItem as coments).Rating == 5)
        {
            img1.ImageUrl = "./galeria/estrela5.png";
        }

        Literal lit1 = (Literal)e.Item.FindControl("litComent");
        lit1.Text = (e.Item.DataItem as coments).Txt;
    }

    protected void Repeater2_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //gera os itens da galeria.
        HtmlAnchor img = (HtmlAnchor)e.Item.FindControl("galBut");
        img.HRef = (e.Item.DataItem as byproduct).Img;
        img.Attributes.Add("rel", "useZoom:'zoom1', smallImage:'" + (e.Item.DataItem as byproduct).Img + "'");

      

        //preencehe o thumb apartir do endereço do banco
        HtmlImage thumb = (HtmlImage)e.Item.FindControl("imgThumb");
        thumb.Src = ((e.Item.DataItem as byproduct).Thumb);



    }


    //GERA THUMB NOVO
    public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
    {
        System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(MapPath(OriginalFile));

        // Prevent using images internal thumbnail
        FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
        FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

        if (OnlyResizeIfWider)
        {
            if (FullsizeImage.Width <= NewWidth)
            {
                NewWidth = FullsizeImage.Width;
            }
        }

        int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
        if (NewHeight > MaxHeight)
        {
            // Resize with height instead
            NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
            NewHeight = MaxHeight;
        }

        System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

        // Clear handle to original file so that we can overwrite it if necessary
        FullsizeImage.Dispose();


        // Save resized picture
        NewImage.Save(MapPath("./galeria/produtos/" + nome + "_small" + ext));
    }


    //Açao do botão votar(cadastrar dados no banco).
    public void LinkButton_Command(Object sender, CommandEventArgs e)
    {
        string rating = Hidden1.Value;
        if (rating.Equals(""))
        {
            rating = "myrating=3";
        }
        string rating2 = rating.Substring(9);
        int id = int.Parse(Session["auxComent"].ToString());
        using (guedesEntities _db = new guedesEntities())
        {
            _db.AddTocoments(new coments { Name = txtName.Text, Txt = txtComent.Text, Rating = int.Parse(rating2), ID_Prod = id, Permission = false });
            _db.SaveChanges();
        }
    }

}